
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Django 管理文档生成器 &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" />
    <link rel="next" title="管理的 JavaScript 自定义" href="javascript.html" />
    <link rel="prev" title="管理动作" href="actions.html" />



 
<script src="../../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="actions.html" title="管理动作">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="javascript.html" title="管理的 JavaScript 自定义">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-admin-admindocs">
            
  <div class="section" id="s-module-django.contrib.admindocs">
<span id="s-the-django-admin-documentation-generator"></span><span id="module-django.contrib.admindocs"></span><span id="the-django-admin-documentation-generator"></span><h1>Django 管理文档生成器<a class="headerlink" href="#module-django.contrib.admindocs" title="永久链接至标题">¶</a></h1>
<p>Django 的 <a class="reference internal" href="#module-django.contrib.admindocs" title="django.contrib.admindocs: Django's admin documentation generator."><code class="xref py py-mod docutils literal notranslate"><span class="pre">admindocs</span></code></a> 应用程序从 <a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 中的任何应用程序的模型、视图、模板标签和模板过滤器的 docstrings 中提取文档，并在 <a class="reference internal" href="index.html#module-django.contrib.admin" title="django.contrib.admin: Django's admin site."><code class="xref py py-mod docutils literal notranslate"><span class="pre">Django</span> <span class="pre">admin</span></code></a> 中提供这些文档。</p>
<div class="section" id="s-overview">
<span id="overview"></span><h2>概况<a class="headerlink" href="#overview" title="永久链接至标题">¶</a></h2>
<p>要激活 <a class="reference internal" href="#module-django.contrib.admindocs" title="django.contrib.admindocs: Django's admin documentation generator."><code class="xref py py-mod docutils literal notranslate"><span class="pre">admindocs</span></code></a>，你需要执行以下操作：</p>
<ul class="simple">
<li>将 <a class="reference internal" href="#module-django.contrib.admindocs" title="django.contrib.admindocs: Django's admin documentation generator."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.admindocs</span></code></a> 添加到你的 <a class="reference internal" href="../../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 中。</li>
<li>将 <code class="docutils literal notranslate"><span class="pre">path('admin/doc/',</span> <span class="pre">include('django.contrib.admindocs.urls'))</span></code> 加入到你的 <code class="docutils literal notranslate"><span class="pre">urlpatterns</span></code> 中。确保它被包含在 <code class="docutils literal notranslate"><span class="pre">'admin/'</span></code> 条目之前，这样对 <code class="docutils literal notranslate"><span class="pre">/admin/doc/</span></code> 的请求就不会被后者处理。</li>
<li>安装 docutils Python 模块（<a class="reference external" href="https://docutils.sourceforge.io/">https://docutils.sourceforge.io/</a>）。</li>
<li><strong>可选的：</strong> 使用 admindocs 书签需要安装 <code class="docutils literal notranslate"><span class="pre">django.contrib.admindocs.middleware.XViewMiddleware</span></code>。</li>
</ul>
<p>一旦这些步骤完成，你就可以进入你的管理界面，点击页面右上方的“文档”链接，开始浏览文档。</p>
</div>
<div class="section" id="s-documentation-helpers">
<span id="documentation-helpers"></span><h2>文件辅助功能<a class="headerlink" href="#documentation-helpers" title="永久链接至标题">¶</a></h2>
<p>下面的特殊标记可以在你的 docstrings 中使用，以方便创建超链接到其他组件。</p>
<table class="docutils">
<colgroup>
<col width="31%" />
<col width="69%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Django 组件</th>
<th class="head">reStructuredText 角色</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>模型</td>
<td><code class="docutils literal notranslate"><span class="pre">:model:`app_label.ModelName`</span></code></td>
</tr>
<tr class="row-odd"><td>视图</td>
<td><code class="docutils literal notranslate"><span class="pre">:view:`app_label.view_name`</span></code></td>
</tr>
<tr class="row-even"><td>模板标签</td>
<td><code class="docutils literal notranslate"><span class="pre">:tag:`tagname`</span></code></td>
</tr>
<tr class="row-odd"><td>模板过滤器</td>
<td><code class="docutils literal notranslate"><span class="pre">:filter:`filtername`</span></code></td>
</tr>
<tr class="row-even"><td>模板</td>
<td><code class="docutils literal notranslate"><span class="pre">:template:`path/to/template.html`</span></code></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="s-model-reference">
<span id="model-reference"></span><h2>模型参考<a class="headerlink" href="#model-reference" title="永久链接至标题">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">admindocs</span></code> 页面的 <strong>模型</strong> 部分描述了系统中的每个模型及其所有字段、属性和可用方法。与其他模型的关系以超链接形式出现。描述来自字段的 <code class="docutils literal notranslate"><span class="pre">help_text</span></code> 属性或模型方法的 docstrings。</p>
<p>一个具有有用文档的模型可能是这样的：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">BlogEntry</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Stores a single blog entry, related to :model:`blog.Blog` and</span>
<span class="sd">    :model:`auth.User`.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">slug</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">SlugField</span><span class="p">(</span><span class="n">help_text</span><span class="o">=</span><span class="s2">&quot;A short label, generally used in URLs.&quot;</span><span class="p">)</span>
    <span class="n">author</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span>
        <span class="n">User</span><span class="p">,</span>
        <span class="n">models</span><span class="o">.</span><span class="n">SET_NULL</span><span class="p">,</span>
        <span class="n">blank</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">blog</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Blog</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">CASCADE</span><span class="p">)</span>
    <span class="o">...</span>

    <span class="k">def</span> <span class="nf">publish</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Makes the blog entry live on the site.&quot;&quot;&quot;</span>
        <span class="o">...</span>
</pre></div>
</div>
</div>
<div class="section" id="s-view-reference">
<span id="view-reference"></span><h2>视图参考<a class="headerlink" href="#view-reference" title="永久链接至标题">¶</a></h2>
<p>在你的网站中，每个 URL 在 <code class="docutils literal notranslate"><span class="pre">admindocs</span></code> 页面中都有一个单独的条目，点击给定的 URL 会显示相应的视图。你可以在视图函数 docstrings 中记录的有用内容包括：</p>
<ul class="simple">
<li>简要说明该视图的作用。</li>
<li>视图模板中的 <strong>上下文</strong>，或可用的变量列表。</li>
<li>用于该视图的一个或多个模板的名称。</li>
</ul>
<p>例子：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.shortcuts</span> <span class="kn">import</span> <span class="n">render</span>

<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">MyModel</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">slug</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Display an individual :model:`myapp.MyModel`.</span>

<span class="sd">    **Context**</span>

<span class="sd">    ``mymodel``</span>
<span class="sd">        An instance of :model:`myapp.MyModel`.</span>

<span class="sd">    **Template:**</span>

<span class="sd">    :template:`myapp/my_template.html`</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">context</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;mymodel&#39;</span><span class="p">:</span> <span class="n">MyModel</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">slug</span><span class="o">=</span><span class="n">slug</span><span class="p">)}</span>
    <span class="k">return</span> <span class="n">render</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s1">&#39;myapp/my_template.html&#39;</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="s-template-tags-and-filters-reference">
<span id="template-tags-and-filters-reference"></span><h2>模板标签和过滤器参考<a class="headerlink" href="#template-tags-and-filters-reference" title="永久链接至标题">¶</a></h2>
<p><strong>标签</strong> 和 <strong>过滤器</strong> <code class="docutils literal notranslate"><span class="pre">admindocs</span></code> 部分描述了所有 Django 自带的标签和过滤器（事实上， <a class="reference internal" href="../../templates/builtins.html#ref-templates-builtins-tags"><span class="std std-ref">内置标签参考</span></a> 和 <a class="reference internal" href="../../templates/builtins.html#ref-templates-builtins-filters"><span class="std std-ref">内置过滤器参考</span></a> 文档直接来自这些页面）。任何你创建的或由第三方应用添加的标签或过滤器也会出现在这些章节中。</p>
</div>
<div class="section" id="s-template-reference">
<span id="template-reference"></span><h2>模板参考<a class="headerlink" href="#template-reference" title="永久链接至标题">¶</a></h2>
<p>虽然 <code class="docutils literal notranslate"><span class="pre">admindocs</span></code> 并不包含记录模板本身的地方，但如果你在 docstring 中使用 <code class="docutils literal notranslate"><span class="pre">:template:`path/to/template.html</span></code> 语法，生成的页面会用 Django 的 <a class="reference internal" href="../../templates/api.html#template-loaders"><span class="std std-ref">模板加载器</span></a> 验证模板的路径。这是个很方便的方法来检查指定的模板是否存在，并显示该模板在文件系统中的位置。</p>
</div>
<div class="section" id="s-included-bookmarklets">
<span id="s-admindocs-bookmarklets"></span><span id="included-bookmarklets"></span><span id="admindocs-bookmarklets"></span><h2>包含的书签<a class="headerlink" href="#included-bookmarklets" title="永久链接至标题">¶</a></h2>
<p>在 <code class="docutils literal notranslate"><span class="pre">admindocs</span></code> 页面上有一个书签：</p>
<dl class="docutils">
<dt>关于本页面的文档</dt>
<dd>从任何页面跳转到生成该页面的视图文档。</dd>
</dl>
<p>使用这个书签需要安装 <code class="docutils literal notranslate"><span class="pre">XViewMiddleware</span></code>，并且你以一个 <a class="reference internal" href="../auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><code class="xref py py-class docutils literal notranslate"><span class="pre">User</span></code></a> 的身份登录到 <a class="reference internal" href="index.html#module-django.contrib.admin" title="django.contrib.admin: Django's admin site."><code class="xref py py-mod docutils literal notranslate"><span class="pre">Django</span> <span class="pre">admin</span></code></a>，并且 <a class="reference internal" href="../auth.html#django.contrib.auth.models.User.is_staff" title="django.contrib.auth.models.User.is_staff"><code class="xref py py-attr docutils literal notranslate"><span class="pre">is_staff</span></code></a> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Django 管理文档生成器</a><ul>
<li><a class="reference internal" href="#overview">概况</a></li>
<li><a class="reference internal" href="#documentation-helpers">文件辅助功能</a></li>
<li><a class="reference internal" href="#model-reference">模型参考</a></li>
<li><a class="reference internal" href="#view-reference">视图参考</a></li>
<li><a class="reference internal" href="#template-tags-and-filters-reference">模板标签和过滤器参考</a></li>
<li><a class="reference internal" href="#template-reference">模板参考</a></li>
<li><a class="reference internal" href="#included-bookmarklets">包含的书签</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="actions.html"
                        title="上一章">管理动作</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="javascript.html"
                        title="下一章">管理的 JavaScript 自定义</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/ref/contrib/admin/admindocs.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="actions.html" title="管理动作">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="javascript.html" title="管理的 JavaScript 自定义">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>